package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.cache.RedisKeyPrefix;
import com.juyu.miliao.api.dao.mysql.OfficialQuestionInfoDao;
import com.juyu.miliao.api.domain.OfficialQuestionInfo;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.constant.OfficialQuestionInfoRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author xuanqicheng
 * @create 2018-18-5:51 PM
 */
@Component
public class OfficialQuestionInfoRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OfficialQuestionInfoDao officialQuestionInfoDao;

    public OfficialQuestionInfo getNewOfficialQuestionInfo() {

        String key = OfficialQuestionInfoRedisKey.NEW_OFFICIAL_QUESTION_INFO;

        String temp = stringRedisTemplate.opsForValue().get(key);

        if (!StringUtils.isEmpty(temp)) {
            return JSONObject.parseObject(temp, OfficialQuestionInfo.class);
        } else {
            OfficialQuestionInfo officialQuestionInfo = officialQuestionInfoDao.getOneQuestionInfo();
            if (officialQuestionInfo != null) {
                stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(officialQuestionInfo),
                        15, TimeUnit.MINUTES);
            }

            return officialQuestionInfo;
        }
    }

    /**
     * 分页数据
     * @param page
     * @param pageSize
     * @return
     */
    public List<OfficialQuestionInfo> getOfficialQuestionInfoList(Integer page, Integer pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);

        String key = OfficialQuestionInfoRedisKey.OFFICIAL_QUESTION_PAGE_INFO+page+":"+pageSize;

        String temp = stringRedisTemplate.opsForValue().get(key);
        if(!StringUtils.isEmpty(temp)){
           List<OfficialQuestionInfo> list = new ArrayList<>(0);

            JSONArray jsonArray = JSONObject.parseArray(temp);
            for(int i=0;i<jsonArray.size();i++){

                String str=jsonArray.getString(i);

                OfficialQuestionInfo officialQuestionInfo = JSONObject.parseObject(str,OfficialQuestionInfo.class);

                list.add(officialQuestionInfo);
            }
            return list;
        }else{

            Map<String,Object> parameterMap = new HashMap<>(0);

            parameterMap.put("startNum",startNum);

            parameterMap.put("pageSize",pageSize);

            List<OfficialQuestionInfo> list = officialQuestionInfoDao.selectPage(parameterMap);
            if(list!=null&&list.size()>0){

                stringRedisTemplate.opsForValue().set(key,JSONObject.toJSONString(list),15,TimeUnit.MINUTES);
            }

            return list;

        }

    }

    public OfficialQuestionInfo getOfficialQuestionByQuestionId(Integer questionId) {

        String key = OfficialQuestionInfoRedisKey.OFFICIAL_QUESTION_INFO_BY_QUESTIONID+questionId;

        String temp = stringRedisTemplate.opsForValue().get(key);

        if(!StringUtils.isEmpty(temp)){
            return JSONObject.parseObject(temp,OfficialQuestionInfo.class);
        }else{

            OfficialQuestionInfo officialQuestionInfo=officialQuestionInfoDao.selectOneById(questionId);
            if(officialQuestionInfo!=null){

                stringRedisTemplate.opsForValue().set(key,JSONObject.toJSONString(officialQuestionInfo),15,
                        TimeUnit.MINUTES);

            }

            return officialQuestionInfo;
        }
    }
}
